﻿Imports System.Collections.Generic
Imports System.Configuration
Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports ATS.Business.Entities
Imports ATS.Services.Contracts
Imports System.Diagnostics


Imports ATS.UI.Process.ATSServiceReference
Imports System.Collections.ObjectModel

' NOTE:
'
' User Process Components are used to abstract the common processing task from 
' the UI or control the UI navigation logic. You can also treat UPCs as a
' Controller (the Entities as the Model and the UI Forms as Views).


Public Class SubmitterUserProcess
    Implements ISubmitterUserProcess

    Public Sub New()
    End Sub

#Region "Assets"
    ' ''' <summary>
    ' ''' Submit an expense.
    ' ''' </summary>
    ' ''' <param name="expense">An Expense instance.</param>
    'Public Sub SubmitExpense(ByVal expense As Expense)
    '    ' Submit the expense.
    '    Try
    '        expense.WorkflowID = Guid.NewGuid()

    '        Dim requestMessage As New ExpenseWorkflowService.SubmitterRequestMessage(expense.WorkflowID, expense)

    '        Dim proxy As New ExpenseWorkflowServiceClient()
    '        proxy.SubmitExpense(requestMessage)
    '        proxy.Close()
    '    Catch ex As FaultException
    '        Throw New ApplicationException(ex.Message)
    '    End Try
    'End Sub

    ' ''' <summary>
    ' ''' Cancel a pending expense.
    ' ''' </summary>
    ' ''' <param name="expense">An Expense instance.</param>
    ' ''' <returns>A boolean value indicating the request has been made.</returns>
    'Public Function CancelExpense(ByVal expense As Expense) As Boolean
    '    Dim result As Boolean = False

    '    ' Cancel the expense.
    '    Try
    '        Dim requestMessage As New ExpenseWorkflowService.SubmitterRequestMessage(expense.WorkflowID, expense)

    '        Dim proxy As New ExpenseWorkflowServiceClient()

    '        proxy.CancelExpense(requestMessage)
    '    Catch ex As FaultException
    '        Throw New ApplicationException(ex.Message)
    '    End Try

    '    result = True

    '    Return result
    'End Function

    'Public Sub ResetDatabase()
    '    Try
    '        Dim proxy As New ExpenseServiceClient()

    '        ' Get a list of active expenses.
    '        Dim expensesList As List(Of Expense) = proxy.ListActiveExpenses()

    '        If expensesList.Count > 0 Then
    '            Throw New ApplicationException("Cannot reset ExpenseSample database when there are active expenses. Please Cancel all active expenses and then try again.")
    '        End If

    '        proxy.Purge()
    '        proxy.Close()
    '    Catch ex As FaultException
    '        Throw New ApplicationException(ex.Message)
    '    End Try
    'End Sub

    'Public Function LoadCategories() As String()
    '    Return [Enum].GetNames(GetType(ExpenseCategory))
    'End Function

    ''' <summary>
    ''' Loads the expenses.
    ''' </summary>
    ''' <param name="employeeID">An EmployeeID.</param>
    ''' <returns>A List of Expenses.</returns>
    Public Function LoadAssets(ByVal employeeID As String) As List(Of MainAssetsTable) Implements ISubmitterUserProcess.LoadAssets
        Dim assetsList As List(Of MainAssetsTable) = Nothing

        Try
            Dim proxy As New AssetServiceClient
            'assetsList = proxy.ListActiveAssets()
            Return assetsList
        Catch ex As FaultException
            Throw New ApplicationException(ex.Message)
        End Try
    End Function
    Public Function CreateAsset(_AssetDetails As MainAssetsTable) As MainAssetsTable Implements ISubmitterUserProcess.CreateAsset
        Try
            Dim cc As New AssetServiceClient()
            Return cc.CreateAsset(_AssetDetails)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function

    Public Function GetAssetDetails(_searchString As String) As MainAssetsTable Implements ISubmitterUserProcess.GetAssetDetails
        Try
            Dim cc As New AssetServiceClient()
            Return cc.GetAssetDetails(_searchString)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function

    Public Function GetAllAssets() As List(Of MainAssetsTable) Implements ISubmitterUserProcess.GetAllAsset
        Try
            Dim cc As New AssetServiceClient()
            Dim a As List(Of MainAssetsTable) = cc.GetAllAssets
            Return cc.GetAllAssets
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function

    
    Public Function UpdateAssetDetails(_AssetDetails As MainAssetsTable) As MainAssetsTable Implements ISubmitterUserProcess.UpdateAssetDetails
        Try
            Dim cc As New AssetServiceClient()
            Return cc.UpdateAssetDetails(_AssetDetails)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function


    Public Sub DeleteAsset(_AssetDetails As MainAssetsTable) Implements ISubmitterUserProcess.DeleteAsset
        Try
            Dim cc As New AssetServiceClient()
            cc.DeleteAsset(_AssetDetails)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Sub
#End Region

#Region "Users"
    Public Function AuthenticateUser(ByVal username As String, ByVal password As String) As AuthenticationResult Implements ISubmitterUserProcess.AuthenticateUser
        Dim authResult As AuthenticationResult = Nothing

        Try
            Dim proxy As New AssetServiceClient
            authResult = proxy.AuthenticateUser(username, password)
            Return authResult
        Catch ex As FaultException
            Throw New ApplicationException(ex.Message)
        End Try
    End Function

    Public Function VerifyHash(encryptedPassword As String, hash As String, salt As String) As Boolean Implements ISubmitterUserProcess.VerifyHash
        Dim hashVerified As Boolean

        Try
            Dim proxy As New AssetServiceClient
            hashVerified = proxy.VerifyHash(encryptedPassword, hash, salt)
            Return hashVerified
        Catch ex As FaultException
            Throw New ApplicationException(ex.Message)
        End Try
    End Function

    Public Function GetUserDetails(ByVal username As String) As UserTable Implements ISubmitterUserProcess.GetUserDetails
        Dim userDetails As UserTable = Nothing

        Try
            Dim proxy As New AssetServiceClient
            Return proxy.GetUserDetails(username)
        Catch ex As FaultException
            Throw New ApplicationException(ex.Message)
        End Try
    End Function

    Public Function GetAllUsers() As List(Of UserTable) Implements ISubmitterUserProcess.GetAllUsers
        Dim userDetails As List(Of UserTable) = Nothing

        Try
            Dim proxy = New AssetServiceClient
            proxy.ClientCredentials.UserName.UserName = "t"
            proxy.ClientCredentials.UserName.Password = "t"
            Return proxy.GetAllUsers
        Catch ex As FaultException
            Throw New ApplicationException(ex.Message)
        End Try
    End Function

    Public Function IObservableListGetAllUsers() As IObserveUserTable Implements ISubmitterUserProcess.IObservableListGetAllUsers
        Try
            Dim proxy As New AssetServiceClient

            Return New ObservableCollection(Of UserTable)(proxy.IObservableListGetAllUsers)
        Catch ex As FaultException
            Throw New ApplicationException(ex.Message)
        End Try
    End Function

    Public Function ChangePassword(userDetail As UserTable) As UserTable Implements ISubmitterUserProcess.ChangePassword
        Dim userDetails As UserTable = Nothing

        Try
            Dim proxy As New AssetServiceClient
            Return proxy.UpdatePassword(userDetail)
        Catch ex As FaultException
            Throw New ApplicationException(ex.Message)
        End Try
    End Function

    Public Function CreateNewUSer(userDetail As UserTable) As UserTable Implements ISubmitterUserProcess.CreateNewUser
        Dim userDetails As UserTable = Nothing

        Try
            Dim proxy As New AssetServiceClient
            Return proxy.CreateNewUser(userDetail)
        Catch ex As FaultException
            Throw New ApplicationException(ex.Message)
        End Try
    End Function

    Public Function UpdateUSer(userDetail As UserTable) As UserTable Implements ISubmitterUserProcess.UpdateUser
        Dim userDetails As UserTable = Nothing

        Try
            Dim proxy As New AssetServiceClient
            Return proxy.UpdateUser(userDetail)
        Catch ex As FaultException
            Throw New ApplicationException(ex.Message)
        End Try
    End Function
    Public Sub DeleteUser(userDetail As UserTable) Implements ISubmitterUserProcess.DeleteUser
        Dim userDetails As UserTable = Nothing

        Try
            Dim proxy As New AssetServiceClient
            proxy.DeleteUser(userDetail)
        Catch ex As FaultException
            Throw New ApplicationException(ex.Message)
        End Try
    End Sub

    Public Function UserExists(username As String) As Boolean Implements ISubmitterUserProcess.UserExists
        Try
            Dim proxy As New AssetServiceClient
            Return proxy.userexists(username)
        Catch ex As FaultException
            Throw New ApplicationException(ex.Message)
        End Try
    End Function
#End Region

#Region "Account Policy"
    Public Function AccountPolicyValues() As admPasswordConfig Implements ISubmitterUserProcess.AccountPolicyValues
        Dim accountPolicyValue As admPasswordConfig = Nothing

        Try
            Dim proxy As New AssetServiceClient
            accountPolicyValue = proxy.AccountPolicyValues
            Return accountPolicyValue
        Catch ex As FaultException
            Throw New ApplicationException(ex.Message)
        End Try
    End Function

    Public Function UpdateAccountPolicy(accountPolicyValue As admPasswordConfig) As admPasswordConfig Implements ISubmitterUserProcess.UpdateAccountPolicy
        Try
            Dim proxy As New AssetServiceClient
            accountPolicyValue = proxy.UpdateAccountPolicy(accountPolicyValue)
            Return accountPolicyValue
        Catch ex As FaultException
            Throw New ApplicationException(ex.Message)
        End Try
    End Function
#End Region

#Region "Company Detail"
    Public Function CreateNewCompanyDetail(ByVal companyDetails As CompanyDetail) As CompanyDetail Implements ISubmitterUserProcess.CreateNewCompanyDetail
        Try
            Dim proxy As New AssetServiceClient
            Return proxy.CreateNewCompanyDetail(companyDetails)
        Catch ex As FaultException
            Throw New ApplicationException(ex.Message)
        End Try
    End Function

    Public Function ReadCompanyDetail() As CompanyDetail Implements ISubmitterUserProcess.ReadCompanyDetail
        Try
            Dim proxy As New AssetServiceClient
            Return proxy.ReadCompanyDetail()
        Catch ex As FaultException
            Throw New ApplicationException(ex.Message)
        End Try
    End Function

    Public Function UpdateCompanyDetail(ByVal companyDetails As CompanyDetail) As CompanyDetail Implements ISubmitterUserProcess.UpdateCompanyDetail
        Try
            Dim proxy As New AssetServiceClient
            Return proxy.UpdateCompanyDetail(companyDetails)
        Catch ex As FaultException
            Throw New ApplicationException(ex.Message)
        End Try
    End Function
#End Region

#Region "RFID Scan History"
    Public Sub CreateRFIDScanHistory(_RFIDScanHistory As RFIDScanHistory, Optional _CreateAssetData As Boolean = False, Optional _CreatedBy As String = Nothing) Implements ISubmitterUserProcess.CreateRFIDScanHistory
        Try
            Dim proxy As New AssetServiceClient
            proxy.CreateRFIDScanHistory(_RFIDScanHistory, _CreateAssetData, _CreatedBy)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Sub

    Public Function ReadRFIDScanHistoryByRFIDNumber(_RFIDNumber As String, Optional _RFIDScanDate As DateTime = Nothing) As List(Of RFIDScanHistory) Implements ISubmitterUserProcess.ReadRFIDScanHistoryByRFIDNumber

        Try
            Dim proxy As New AssetServiceClient
            Return proxy.ReadRFIDScanHistoryByRFIDNumber(_RFIDNumber, _RFIDScanDate)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function

    Public Function ReadRFIDScanHistoryByScanDate(_scanDateFrom As Date, _scanDateTo As Date) As List(Of RFIDScanHistory) Implements ISubmitterUserProcess.ReadRFIDScanHistoryByScanDate
        Try
            Dim proxy As New AssetServiceClient
            Return proxy.ReadRFIDScanHistoryByScanDate(_scanDateFrom, _scanDateTo)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function
#End Region

#Region "Category"
    Public Function CreateCategory(_category As Category) As Category Implements ISubmitterUserProcess.CreateCategory
        Try
            Dim cc As New AssetServiceClient()
            Return cc.CreateCategory(_category)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function

    Public Function ReadCategory(_category As String) As Category Implements ISubmitterUserProcess.ReadCategory
        Try
            Dim cc As New AssetServiceClient()
            Return cc.ReadCategory(_category)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function

    Public Function ReadAllCategories() As List(Of Category) Implements ISubmitterUserProcess.ReadAllCategories
        Try
            Dim cc As New AssetServiceClient()
            Return cc.ReadAllCategories()
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function

    Public Function GetAllCategoryClass() As List(Of String) Implements ISubmitterUserProcess.GetAllCategoryClass
        Try
            Dim cc As New AssetServiceClient()
            Return cc.GetAllCategoryClass()
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function

    Public Function UpdateCategoryRecord(_category As Category) As Category Implements ISubmitterUserProcess.UpdateCategoryRecord
        Try
            Dim cc As New AssetServiceClient()
            Return cc.UpdateCategoryRecord(_category)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function


    Public Sub DeleteCategory(_category As Category) Implements ISubmitterUserProcess.DeleteCategory
        Try
            Dim cc As New AssetServiceClient()
            cc.DeleteCategory(_category)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Sub
#End Region

#Region "Location"
    Public Function CreateLocation(_location As Location) As Location Implements ISubmitterUserProcess.CreateLocation
        Try
            Dim cc As New AssetServiceClient()
            Return cc.CreateLocation(_location)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function

    Public Function ReadLocation(_location As String) As Location Implements ISubmitterUserProcess.ReadLocation
        Try
            Dim cc As New AssetServiceClient()
            Return cc.ReadLocation(_location)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function

    Public Function ReadAllLocations() As List(Of Location) Implements ISubmitterUserProcess.ReadAllLocations
        Try
            Dim cc As New AssetServiceClient()
            Return cc.ReadAllLocations()
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function

    Public Function UpdateLocationRecord(_location As Location) As Location Implements ISubmitterUserProcess.UpdateLocationRecord
        Try
            Dim cc As New AssetServiceClient()
            Return cc.UpdateLocationRecord(_location)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function


    Public Sub DeleteLocation(_location As Location) Implements ISubmitterUserProcess.DeleteLocation
        Try
            Dim cc As New AssetServiceClient()
            cc.DeleteLocation(_location)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Sub
#End Region

#Region "Person"
    Public Function CreatePerson(_personDetails As Person) As Person Implements ISubmitterUserProcess.CreatePerson
        Try
            Dim cc As New AssetServiceClient()
            Return cc.CreatePerson(_personDetails)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function

    Public Function GetPersonDetails(_searchString As String) As Person Implements ISubmitterUserProcess.GetPersonDetails
        Try
            Dim cc As New AssetServiceClient()
            Return cc.GetPersonDetails(_searchString)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function

    Public Function GetAllPersons() As List(Of Person) Implements ISubmitterUserProcess.GetAllPerson
        Try
            Dim cc As New AssetServiceClient()
            Return cc.GetAllPersons
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function

    Public Function UpdatePersonDetails(_personDetails As Person) As Person Implements ISubmitterUserProcess.UpdatePersonDetails
        Try
            Dim cc As New AssetServiceClient()
            Return cc.UpdatePersonDetails(_personDetails)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function


    Public Sub DeletePerson(_personDetails As Person) Implements ISubmitterUserProcess.DeletePerson
        Try
            Dim cc As New AssetServiceClient()
            cc.DeletePerson(_personDetails)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Sub
#End Region


#Region "Department"
    Public Function CreateDepartment(_DepartmentDetails As Department) As Department Implements ISubmitterUserProcess.CreateDepartment
        Try
            Dim cc As New AssetServiceClient()
            Return cc.CreateDepartment(_DepartmentDetails)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function

    Public Function GetDepartmentDetails(_searchString As String) As Department Implements ISubmitterUserProcess.GetDepartmentDetails
        Try
            Dim cc As New AssetServiceClient()
            Return cc.GetDepartmentDetails(_searchString)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function

    Public Function GetAllDepartments() As List(Of Department) Implements ISubmitterUserProcess.GetAllDepartment
        Try
            Dim cc As New AssetServiceClient()
            Return cc.GetAllDepartments
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function

    Public Function GetDepartmentList() As List(Of String) Implements ISubmitterUserProcess.GetDepartmentList
        Try
            Dim cc As New AssetServiceClient()
            Return cc.GetDepartmentList
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function

    Public Function UpdateDepartmentDetails(_DepartmentDetails As Department) As Department Implements ISubmitterUserProcess.UpdateDepartmentDetails
        Try
            Dim cc As New AssetServiceClient()
            Return cc.UpdateDepartmentDetails(_DepartmentDetails)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function


    Public Sub DeleteDepartment(_DepartmentDetails As Department) Implements ISubmitterUserProcess.DeleteDepartment
        Try
            Dim cc As New AssetServiceClient()
            cc.DeleteDepartment(_DepartmentDetails)
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Sub
#End Region

#Region "Disposal Methods Lsit"
    Public Function GetDisposalMethods() As List(Of String) Implements ISubmitterUserProcess.GetDisposalMethods
        Try
            Dim cc As New AssetServiceClient()
            Return cc.GetDisposalMethods()
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function
#End Region
#Region "Depreciation List"
    Public Function DepreciationList() As List(Of DepreciationClass) Implements ISubmitterUserProcess.DepreciationList
        Try
            Dim cc As New AssetServiceClient()
            Return cc.DepreciationList()
        Catch ex As Exception
            Throw New FaultException(Of ProcessExecutionFault)(New ProcessExecutionFault(), ex.Message)
        End Try
    End Function
#End Region


    'Public Function LoadExpenseReviews(ByVal expenseID As Long) As List(Of ExpenseReview)
    '    Dim reviewsList As List(Of ExpenseReview) = Nothing

    '    Try
    '        Dim proxy As New ExpenseServiceClient()
    '        reviewsList = proxy.ListExpenseReviews(expenseID)
    '        proxy.Close()
    '    Catch ex As FaultException
    '        Throw New ApplicationException(ex.Message)
    '    End Try
    '    Return reviewsList
    'End Function

    'Public Function LoadExpenseLogs(ByVal expenseID As Long) As List(Of ExpenseLog)
    '    Dim LogsList As List(Of ExpenseLog) = Nothing
    '    Try
    '        Dim proxy As New ExpenseServiceClient()
    '        LogsList = proxy.ListExpenseLogs(expenseID)
    '        proxy.Close()
    '    Catch ex As FaultException
    '        Throw New ApplicationException(ex.Message)
    '    End Try

    '    Return LogsList
    'End Function

End Class

